<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of main
 *
 * @author Maulnick
 */
class Message extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $tempsession = $this->session->userdata('user');
        $this->load->helper('url','form');
        $this->load->model('User_Model');
        $this->load->model('Message_Model');
        $this->load->model('Notification_Model');
        
        $this->load->model('Document_Entity');
        $this->load->model('Billoflading_Entity');
        $this->load->model('Freightinvoice_Entity');
        $this->load->model('Deliveryorder_Entity');
        $this->load->model('PIB_Entity');
        $this->load->model('SPPB_Entity');
        $this->load->model('Payment_Entity');
        
        $this->load->library('session');
        $this->load->library('table');
        $this->load->library('grocery_CRUD');
        $this->load->library('document_parser');
        
        //echo($this->date_operation);echo('</BR>');
        $title = 'task';
        $tempsession = $this->session->userdata('user');
        if(!$tempsession){
            show_404();
            return;
        }
    }
    public function index(){
        
    }
    /*** 
     * 
     * /Message/function_page()
     * 
     * ***/
    public function inbox_page(){
        $title = 'inbox';
        $tempsession = $this->session->userdata('user');
        /*** start crud setting ***/
        $crud = $this->get_message('inbox');
        $crud->set_subject('Inbox');
        $crud->columns('is_read','message_id');
        $crud->unset_add();
        $crud->display_as('message_id','message');
        $crud->add_action('View', '', '','',array($this,'call_view_message_inbox_page'));
        /*** end crud setting ***/
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'crud';
        $middlepage['title'] = 'Inbox';
        $middlepage['output'] = $crud->render();
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    public function outbox_page(){
        $title = 'outbox';
        $tempsession = $this->session->userdata('user');
        
        /*** start crud setting ***/
        $crud = $this->get_message('outbox');
        $crud->set_subject('Outbox');
        $crud->columns('is_read','message_id');
        $crud->display_as('message_id','message');
        $crud->unset_add();
        $crud->add_action('View', '', '','',array($this,'call_view_message_outbox_page'));
        /*** end crud setting ***/
        
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'crud';
        $middlepage['title'] = 'Outbox';
        $middlepage['output'] = $crud->render();
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    public function noticeofarrival_management_page(){
        $title = 'noticeofarrival';
        $tempsession = $this->session->userdata('user');
        
        /*** start crud setting ***/
        $crud = $this->get_message();
        $crud->set_subject('Notice of Arrival');
        $crud->where('subject','noticeofarrival');
        if($tempsession['role_id']==ROLE_CONSIGNEE){
            $crud->unset_add();
        }else if($tempsession['role_id']==ROLE_SHIPPINGLINE){
            $crud->fields('to','quick_message','body','file_url1','file_url2');
            $crud->required_fields('to','file_url1','file_url2');
            $this->load->config('grocery_crud');
            $this->config->set_item('grocery_crud_file_upload_allow_file_types','xml|xml');
            
            $crud->callback_add_field('to',array($this,'add_consignee_recipient'));
            $crud->callback_add_field('quick_message',array($this,'add_quick_message'));
            
            $crud->set_field_upload('file_url1','assets/uploads/files');
            $crud->display_as('file_url1','Freight Invoice');
            
            $crud->set_field_upload('file_url2','assets/uploads/files');
            $crud->display_as('file_url2','Delivery Order');
            
            $crud->callback_insert(array($this,'save_noticeofarrival'));
        }
        /*** end crud setting ***/
        
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'crud';
        $middlepage['title'] = 'Send Notification Of Arrival';
        $middlepage['output'] = $crud->render();
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    
     public function message_management_page(){
        $title = 'message';
        $tempsession = $this->session->userdata('user');
        
        /*** start crud setting ***/
        $crud = $this->get_message();
        $crud->set_subject('Message');
        $crud->set_relation('message_id','message_document','{role_document_id}');
        
        $crud->fields('to','subject','quick_message','body');
        $this->load->config('grocery_crud');

        $crud->callback_add_field('to',array($this,'add_all_recipient'));
        $crud->callback_add_field('quick_message',array($this,'add_quick_message'));

        $crud->callback_insert(array($this,'save_message'));
        
        /*** end crud setting ***/
        
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'crud';
        $middlepage['title'] = 'Send Message';
        $middlepage['output'] = $crud->render();
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    
    public function freightinvoice_management_page(){
        $title = 'freightinvoice';
        $tempsession = $this->session->userdata('user');
        
        /*** start crud setting ***/
        $crud = $this->get_message();
        $crud->set_subject('Freight Invoice');
        $crud->where('subject','freightinvoice');
        $crud->set_relation('message_id','message_document','{role_document_id}{file_url}');
        if($tempsession['role_id']==ROLE_CONSIGNEE){
            $crud->unset_add();
        }else if($tempsession['role_id']==ROLE_SHIPPINGLINE){
            $crud->fields('to','quick_message','body','file_url1');
            $this->load->config('grocery_crud');
            $this->config->set_item('grocery_crud_file_upload_allow_file_types',
                                                            'xml|xml');
            
            $crud->callback_add_field('to',array($this,'add_consignee_recipient'));
            $crud->callback_add_field('quick_message',array($this,'add_quick_message'));
            
            $crud->set_field_upload('file_url1','assets/uploads/files');
            $crud->display_as('file_url1','Freight Invoice');
            
            $crud->callback_insert(array($this,'save_freightinvoice'));
        }
        /*** end crud setting ***/
        
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'crud';
        $middlepage['title'] = 'Send Freight Invoice';
        $middlepage['output'] = $crud->render();
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    
    public function deliveryorder_management_page(){
        $title = 'deliveryorder';
        $tempsession = $this->session->userdata('user');
        
        /*** start crud setting ***/
        $crud = $this->get_message();
        $crud->set_subject('Delivery Order');
        $crud->where('subject','deliveryorder');
        $crud->set_relation('message_id','message_document','{role_document_id}{file_url}');
        if($tempsession['role_id']==ROLE_CONSIGNEE){
            $crud->unset_add();
        }else if($tempsession['role_id']==ROLE_SHIPPINGLINE){
            $crud->fields('to','quick_message','body','file_url1');
            $this->load->config('grocery_crud');
            $this->config->set_item('grocery_crud_file_upload_allow_file_types',
                                                            'xml|xml');
            
            $crud->callback_add_field('to',array($this,'add_consignee_recipient'));
            $crud->callback_add_field('quick_message',array($this,'add_quick_message'));
            
            $crud->set_field_upload('file_url1','assets/uploads/files');
            $crud->display_as('file_url1','Delivery Order');
            
            $crud->callback_insert(array($this,'save_deliveryorder'));
        }
        /*** end crud setting ***/
        
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'crud';
        $middlepage['title'] = 'Send Delivery Order';
        $middlepage['output'] = $crud->render();
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    
    public function billoflading_management_page(){
        $title = 'billoflading';
        $tempsession = $this->session->userdata('user');
        
        /*** start crud setting ***/
        $crud = $this->get_message();
        $crud->set_subject('Bill of Lading');
        $crud->where('subject','billoflading');
        $crud->set_relation('message_id','message_document','{role_document_id}{file_url}');
        if($tempsession['role_id']==ROLE_CONSIGNEE){
            $crud->unset_add();
        }else if($tempsession['role_id']==ROLE_SHIPPINGLINE){
            $crud->fields('to','quick_message','body','file_url1');
            $this->load->config('grocery_crud');
            $this->config->set_item('grocery_crud_file_upload_allow_file_types',
                                                            'xml|xml');
            
            $crud->callback_add_field('to',array($this,'add_consignee_recipient'));
            $crud->callback_add_field('quick_message',array($this,'add_quick_message'));
            $crud->set_field_upload('file_url1','assets/uploads/files');
            $crud->display_as('file_url1','Bill of Lading');
            
            $crud->callback_insert(array($this,'save_billoflading'));
        }
        /*** end crud setting ***/
        
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'crud';
        $middlepage['title'] = 'Send Bill of Lading';
        $middlepage['output'] = $crud->render();
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    
    public function payment_freightinvoice_management_page(){
        $title = 'payment of freightinvoice';
        $tempsession = $this->session->userdata('user');
        
        /*** start crud setting ***/
        $crud = $this->get_message();
        $crud->set_subject('Payment of FreightInvoice');
        $crud->where('subject','paymentoffreightinvoice');
        $crud->set_relation('message_id','message_document','{role_document_id}{file_url}');
        if($tempsession['role_id']==ROLE_CONSIGNEE){
            $crud->fields('to','file_freightinvoice','quick_message','body','file_url1');
            $this->load->config('grocery_crud');
            $this->config->set_item('grocery_crud_file_upload_allow_file_types',
                                                            'jpg|jpeg|bmp|png');
            
            $crud->callback_add_field('to',array($this,'add_shippingline_recipient'));
            $crud->callback_add_field('quick_message',array($this,'add_quick_message'));
            $crud->callback_add_field('file_freightinvoice',array($this,'add_file_freightinvoice'));
            
            $crud->display_as('file_freightinvoice','File Freightinvoice');
            
            $crud->set_field_upload('file_url1','assets/uploads/files');
            $crud->display_as('file_url1','Payment of Freightinvoice');
            
            $crud->callback_insert(array($this,'save_paymentfile_freightinvoice'));
        }
        /*** end crud setting ***/
        
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'crud';
        $middlepage['title'] = 'Send Payment of FreightInvoice';
        $middlepage['output'] = $crud->render();
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    
    public function pib_management_page(){
        $title = 'pib';
        $tempsession = $this->session->userdata('user');
        
        /*** start crud setting ***/
        $crud = $this->get_message();
        $crud->set_subject('PIB');
        $crud->where('subject','pib');
        $crud->set_relation('message_id','message_document','{role_document_id}{file_url}');
        if($tempsession['role_id']==ROLE_INSW){
            $crud->unset_add();
        }else if($tempsession['role_id']==ROLE_CONSIGNEE){
            $crud->fields('to','quick_message','body','file_url1');
            $this->load->config('grocery_crud');
            $this->config->set_item('grocery_crud_file_upload_allow_file_types',
                                                            'xml|xml');
            
            $crud->callback_add_field('to',array($this,'add_insw_recipient'));
            $crud->callback_add_field('quick_message',array($this,'add_quick_message'));
             
            $crud->set_field_upload('file_url1','assets/uploads/files');
            $crud->display_as('file_url1','PIB');
            
            $crud->callback_insert(array($this,'save_pib'));
        }
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'crud';
        $middlepage['title'] = 'Send PIB';
        $middlepage['output'] = $crud->render();
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    
    public function sppb_management_page(){
        $title = 'sppb';
        $tempsession = $this->session->userdata('user');
        
        /*** start crud setting ***/
        $crud = $this->get_message();
        $crud->set_subject('SPPB');
        $crud->where('subject','sppb');
        $crud->set_relation('message_id','message_document','{role_document_id}{file_url}');
        if($tempsession['role_id']==ROLE_CONSIGNEE){
            $crud->unset_add();
        }else if($tempsession['role_id']==ROLE_INSW){
            $crud->fields('to','quick_message','body','file_url1');
            $this->load->config('grocery_crud');
            $this->config->set_item('grocery_crud_file_upload_allow_file_types',
                                                            'xml|xml');
            
            $crud->callback_add_field('to',array($this,'add_consignee_recipient'));
            $crud->callback_add_field('quick_message',array($this,'add_quick_message'));
            
            $crud->set_field_upload('file_url1','assets/uploads/files');
            $crud->display_as('file_url1','SPPB');
            
            $crud->callback_insert(array($this,'save_sppb'));
        }
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'crud';
        $middlepage['title'] = 'Send SPPB';
        $middlepage['output'] = $crud->render();
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    
    public function view_message_page($user_message_map_id){
        $title = 'view message';
        $tempsession = $this->session->userdata('user');
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        
        $user_message_map = $this->Message_Model->get_user_message_map($user_message_map_id);
        $this->Message_Model->update_is_read($user_message_map_id);
        $message_id = $user_message_map->message_id;
        $format = $user_message_map->message_placeholder_id;
        
        $recipient = $this->Message_Model->get_recipient($message_id,$tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'view_message';
        $middlepage['message'] = $this->Message_Model->get_message($message_id);
        $middlepage['document'] = $this->Message_Model->get_message_document($message_id);
        $middlepage['recipient'] = $this->User_Model->get_user($recipient->role_user_id,$recipient->user_id);
        $middlepage['format'] = $format;
        $this->Notification_Model->update_is_read($message_id,$tempsession['role_id'],$tempsession['user_id']);
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    
    public function view_document_page($role_doc_id,$doc_id){
        $title = 'view document';
        if($role_doc_id==DOCUMENT_FREIGHTINVOICE){
            $middlepage['page'] = 'view_freightinvoice';
            $middlepage['freightinvoice'] = $this->Freightinvoice_Entity->load($doc_id);
        }else if($role_doc_id==DOCUMENT_DELIVERYORDER){
            $middlepage['page'] = 'view_deliveryorder';
            $middlepage['deliveryorder'] = $this->Deliveryorder_Entity->load($doc_id);
        }else if($role_doc_id==DOCUMENT_BILLOFLADING){
            $middlepage['page'] = 'view_billoflading';
            $middlepage['billoflading'] = $this->Billoflading_Entity->load($doc_id);
        }else if($role_doc_id==DOCUMENT_PIB){
            $middlepage['page'] = 'view_pib';
            $middlepage['pib'] = $this->PIB_Entity->load($doc_id);
        }else if($role_doc_id==DOCUMENT_SPPB){
            $middlepage['page'] = 'view_sppb';
            $middlepage['sppb'] = $this->SPPB_Entity->load($doc_id);
        }else if($role_doc_id==DOCUMENT_PAYMENTFILE){
            $middlepage['page'] = 'view_payment';
            $middlepage['payment'] = $this->Payment_Entity->load($doc_id);
            $payment = $middlepage['payment'];
            if ($payment->role_document==DOCUMENT_FREIGHTINVOICE){
                $middlepage['document'] = $this->Freightinvoice_Entity->load($payment->document_id);
            }
        }
        $tempsession = $this->session->userdata('user');
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        
        $rightpage['page'] = 'notification';
        $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    
    /*** 
     * 
     * /Message/function()
     * 
     * ***/
    private function get_message($format = 'all'){
        /**
         * i format => all : o => all message, format => inbox : o => inbox message, 
         * format => outbox : o => outbox message 
         */
        $tempsession = $this->session->userdata('user');
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        if($format=='inbox'){
            $crud->set_table('user_message_map');
            $crud->set_relation('message_id','message','| {subject} | {date}');
            $crud->where('role_user_id',$tempsession['role_id']);
            $crud->where('user_id',$tempsession['user_id']);
            $crud->where('message_placeholder_id', MESSAGE_INBOX);
        }else if($format=='outbox'){
            $crud->set_table('user_message_map');
            $crud->set_relation('message_id','message','| {subject} | {date}');
            $crud->where('role_user_id',$tempsession['role_id']);
            $crud->where('user_id',$tempsession['user_id']);
            $crud->where('message_placeholder_id', MESSAGE_OUTBOX);
        }else{
            $crud->set_table('message');
        }
        $crud->unset_edit();
        return $crud;
    }
    private function show_page($title, $leftpage, $middlepage, $rightpage){
        $data['title'] = $title;
        $data['topbar'] = $this->load->view('topbar/home','',TRUE);
        $data['rightpage'] = $this->load->view('mainpage/bodyright',$rightpage,TRUE);
        $data['middlepage'] = $this->load->view('mainpage/bodymiddle',$middlepage,TRUE);
        $data['leftpage'] = $this->load->view('mainpage/bodyleft',$leftpage,TRUE);
        $this->load->view('front',$data);
    }
    private function send_XML($role_document_id, $file_document, $from_role_id, $from_user_id,
            $to_role_id, $to_user_id){
        
        $tempsession = $this->session->userdata('user');
        $realdate = Date('d-m-Y H-i-s');
        
        $to_upload_path=''; 
        
        $to_username=$this->User_Model->get_username_by_role_and_id($to_role_id, $to_user_id);
        if($to_role_id==ROLE_CONSIGNEE){
            $to_upload_path = '.././lis/assets/documents/process/Consignee/'.$to_username.'/Inbox';
        }else if($to_role_id==ROLE_SHIPPINGLINE){
            $to_upload_path = './assets/documents/process/ShippingLine/'.$to_username.'/Inbox';
        }else if($to_role_id==ROLE_OPERATORTERMINAL){
            $to_upload_path = './assets/documents/process/OperatorTerminal/'.$to_username.'/Inbox';
        }else if($to_role_id==ROLE_TRUCKING){
            $to_upload_path = './assets/documents/process/Trucking/'.$to_username.'/Inbox';
        }else if($to_role_id==ROLE_INSW){
            $to_upload_path = '.././insw/assets/documents/process/INSW/'.$to_username.'/Inbox';
        }
        
        $from_upload_path='';
        if($from_role_id==ROLE_CONSIGNEE){
            $from_upload_path = './assets/documents/process/Consignee/'.$tempsession['username'].'/Sent';
        }else if($from_role_id==ROLE_SHIPPINGLINE){
            $from_upload_path = './assets/documents/process/ShippingLine/'.$tempsession['username'].'/Sent';
        }else if($from_role_id==ROLE_OPERATORTERMINAL){
            $from_upload_path = './assets/documents/process/OperatorTerminal/'.$tempsession['username'].'/Sent';
        }else if($from_role_id==ROLE_TRUCKING){
            $from_upload_path = './assets/documents/process/Trucking/'.$tempsession['username'].'/Sent';
        }else if($from_role_id==ROLE_INSW){
            $from_upload_path = '.././insw/assets/documents/process/INSW/INSW/Sent';
        }
        $filename='';
        if($role_document_id==DOCUMENT_FREIGHTINVOICE){
            $filename='FreightInvoice';
        }else if($role_document_id==DOCUMENT_DELIVERYORDER){
            $filename='DeliveryOrder';
        }else if($role_document_id==DOCUMENT_BILLOFLADING){
            $filename='BillofLading';
        }else if($role_document_id==DOCUMENT_PAYMENTFILE){
            $filename='PaymentFile';
        }else if($role_document_id==DOCUMENT_PIB){
            $filename='PIB';
        }else if($role_document_id==DOCUMENT_SPPB){
            $filename='SPPB';
        }
        // 1. sending xml to sender sent folder and inbox folder
        // change file name
        //$file_document = "tes_PIB.xml";
        $ext = end(explode('.', $file_document));
        $filename = '['.$filename.']_'.$realdate.'.'.$ext;
        if(copy('assets/uploads/files/'.$file_document, $to_upload_path.'/'.$filename)
                && copy('assets/uploads/files/'.$file_document,$from_upload_path.'/'.$filename)){
        }else{
            echo 'error copying files';
        }
        // 2. rename file in assets/uploads/file to filename
        if(rename('assets/uploads/files/'.$file_document,'assets/uploads/files/'.$filename)){
        }else{
            echo 'error renaming original files';
        };
        return $filename;
    }
    
    public function download_document($message_id,$role_doc_id,$doc_id,$format_doc="docx",$folder=null){
        $tempsession = $this->session->userdata('user');
        
        if($doc_id=='null'){
            $message_document = $this->Message_Model->get_message_document_by_role_id($message_id,$role_doc_id,null);
        }else{
            $message_document = $this->Message_Model->get_message_document_by_role_id($message_id,$role_doc_id,$doc_id);
        }
        $original_filename = $message_document->file_url;
        $filename = explode('.',$original_filename);
        $new_filename = $filename[0].'.'.$format_doc;
        
        // doc_path
        $doc_path='';
        if($tempsession['role_id']==ROLE_CONSIGNEE){
            $doc_path = './assets/documents/process/Consignee/'.$tempsession['username'].'/Doc/';
        }else if($tempsession['role_id']==ROLE_SHIPPINGLINE){
            $doc_path = './assets/documents/process/ShippingLine/'.$tempsession['username'].'/Doc/';
        }else if($tempsession['role_id']==ROLE_OPERATORTERMINAL){
            $doc_path = './assets/documents/process/OperatorTerminal/'.$tempsession['username'].'/Doc/';
        }else if($tempsession['role_id']==ROLE_TRUCKING){
            $doc_path = './assets/documents/process/Trucking/'.$tempsession['username'].'/Doc/';
        }else if($tempsession['role_id']==ROLE_INSW){
            $doc_path = '.././insw/assets/documents/process/INSW/INSW/Doc/';
        }
        
        // setting path
        $path = $doc_path.$new_filename;
        
        // setting err
        $err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
        if (!file_exists($path)){
           // create new file if file doesn't exist
           if($role_doc_id==DOCUMENT_FREIGHTINVOICE){
               $freightinvoice = $this->Freightinvoice_Entity->load($doc_id);
               $file_template = './assets/documents/template/docx/fi_output.docx';
               $this->document_parser->convert_fiarray_to_docx($freightinvoice, $file_template, $path);
           }else if($role_doc_id==DOCUMENT_DELIVERYORDER){
               $deliveryorder = $this->Deliveryorder_Entity->load($doc_id);
               $file_template = './assets/documents/template/docx/do_output.docx';
               $this->document_parser->convert_doarray_to_docx($deliveryorder, $file_template, $path);
           }else if($role_doc_id==DOCUMENT_BILLOFLADING){
               $billoflading = $this->Billoflading_Entity->load($doc_id);
               $file_template = './assets/documents/template/docx/bl_output.docx';
               $this->document_parser->convert_blarray_to_docx($billoflading, $file_template, $path);
           }else if($role_doc_id==DOCUMENT_PIB){
               $pib = $this->PIB_Entity->load($doc_id);
               //print_r($pib);
               $file_template = './assets/documents/template/docx/pib_output.docx';
               $this->document_parser->convert_pibarray_to_docx($pib, $file_template, $path);
           }else if($role_doc_id==DOCUMENT_SPPB){
               $sppb = $this->SPPB_Entity->load($doc_id);
               $file_template = './assets/documents/template/docx/sppb_output.docx';
               $this->document_parser->convert_sppbarray_to_docx($sppb, $file_template, $path);
           }else if($role_doc_id==DOCUMENT_PAYMENTFILE){
               $doc_path = substr($doc_path,0,-4);
               $doc_path.= $folder.'/';
               $path = $doc_path.$new_filename;
           }
        }
        // download file
        if(file_exists($path)){
            $size = filesize($path);
            header('Content-Type: application/octet-stream');
            header('Content-Length: '.$size);
            header('Content-Disposition: attachment; filename='.$new_filename);
            header('Content-Transfer-Encoding: binary');
            // open the file in binary read-only mode
            // display the error messages if the file can´t be opened
            $file = @ fopen($path, 'rb');
            if ($file) {
                // stream the file and exit the script when complete
                fpassthru($file);
                exit;
            } else {
                echo $err;
            }
        }
    }
    
    public function update_approvement_invoice(){
        $role_document = $_POST['role_document'];
        $document_id = $_POST['document_id'];
        $action = $_POST['action'];
        $payment_id = $_POST['payment_id'];
        if($role_document==DOCUMENT_FREIGHTINVOICE){
            if($this->Freightinvoice_Entity->update_approvement($document_id,$action)){
                if($this->save_approvement_paymentfile($payment_id))
                    redirect('/message/view_document_page/'.$role_document.'/'.$document_id);
            }
        }else{
            
        }
    }
    
    public function update_approvement(){
        $role_document = $_POST['role_document'];
        $document_id = $_POST['document_id'];
        $action = $_POST['action'];
        if($role_document==DOCUMENT_PIB){
            if($this->PIB_Entity->update_approvement($document_id, $action)){
                if($this->save_approvement(DOCUMENT_PIB, $document_id))
                    redirect('/message/view_document_page/'.$role_document.'/'.$document_id);
            }
        }else{
            
        }
    }
    /*** 
     * 
     * /Message/callback_function()
     * 
     * ***/
    function add_all_recipient(){
        $cosignees = $this->User_Model->get_all_consignee();
        $shippinglines = $this->User_Model->get_all_shippingline();
        $all_insw = $this->User_Model->get_all_insw();
        $html = '<select name="to">';
        if ($cosignees){
            foreach($cosignees as $consignee){
                $html .= '<option value='.ROLE_CONSIGNEE.','.$consignee->consignee_id.'>'.$consignee->name.'</option>';
            }
        }
        if ($shippinglines){
            foreach($shippinglines as $shippingline){
                $html .= '<option value='.ROLE_SHIPPINGLINE.','.$shippingline->shippingline_id.'>'.$shippingline->name.'</option>';
            }
        }
        if ($all_insw){
            foreach($all_insw as $insw){
                $html .= '<option value='.ROLE_INSW.','.$insw->insw_id.'>'.$insw->name.'</option>';
            }
        }
        $html .= '</select>';
        return $html;
    }
    function add_consignee_recipient(){
        $cosignees = $this->User_Model->get_all_consignee();
        $html = '';
        if ($cosignees){
            $html = '<select name="to">';
            foreach($cosignees as $consignee){
                $html .= '<option value='.$consignee->consignee_id.'>'.$consignee->name.'</option>';
            }
            $html .= '</select>';
        }
        return $html;
    }
    function add_shippingline_recipient(){
        $shippinglines = $this->User_Model->get_all_shippingline();
        $html = '';
        if ($shippinglines){
            $html = '<select name="to">';
            foreach($shippinglines as $shippingline){
                $html .= '<option value='.$shippingline->shippingline_id.'>'.$shippingline->name.'</option>';
            }
            $html .= '</select>';
        }
        return $html;
    }
    
    function add_insw_recipient(){
        $all_insw = $this->User_Model->get_all_insw();
        $html = '';
        if ($all_insw){
            $html = '<select name="to">';
            foreach($all_insw as $insw){
                $html .= '<option value='.$insw->insw_id.'>'.$insw->name.'</option>';
            }
            $html .= '</select>';
        }
        return $html;
    }
    
    function add_quick_message(){
        $html = '<select name="quick_message">';
        $html .= '<option value=""></option>';
        $html .= '<option value="Dokumen sudah dilampirkan. Mohon direspon secepatnya!">Dokumen sudah dilampirkan. Mohon direspon secepatnya!</option>';
        $html .= '<option value="Bukti pembayaran sudah dilampirkan. Mohon direspon secepatnya!">Bukti pembayaran sudah dilampirkan. Mohon direspon secepatnya!</option>';
        $html .= '<option value="Bukti pembayaran sudah diverifikasi dan valid.">Bukti pembayaran sudah diverifikasi dan valid.</option>';
        $html .= '<option value="Dokumen BL belum dikirimkan. Mohon dikirim secepatnya!">Dokumen BL belum dikirimkan. Mohon dikirim secepatnya!</option>';
        $html .= '<option value="Dokumen FI belum dikirimkan. Mohon dikirim secepatnya!">Dokumen FI belum dikirimkan. Mohon dikirim secepatnya!</option>';
        $html .= '<option value="Dokumen DO belum dikirimkan. Mohon dikirim secepatnya!">Dokumen DO belum dikirimkan. Mohon dikirim secepatnya!</option>';
        $html .= '<option value="Dokumen PIB belum dikirimkan. Mohon dikirim secepatnya!">Dokumen PIB belum dikirimkan. Mohon dikirim secepatnya!</option>';
        $html .= '<option value="Dokumen SPPB belum dikirimkan. Mohon dikirim secepatnya!">Dokumen SPPB belum dikirimkan. Mohon dikirim secepatnya!</option>';
        $html .= '<option value="Bukti Pembayaran belum dikirimkan. Mohon dikirim secepatnya!">Bukti Pembayaran belum dikirimkan. Mohon dikirim secepatnya!</option>';
        $html .= '</select>';
        return $html;
    }
    
    function add_file_freightinvoice(){
        $tempsession = $this->session->userdata('user');
        $freightinvoices = $this->Freightinvoice_Entity->get_freightinvoice_by_owner($tempsession['role_id'],$tempsession['user_id']);
        $html = '';
        if ($freightinvoices){
            $html = '<select name="freightinvoice">';
            foreach($freightinvoices as $freightinvoice){
                $html .= '<option value='.$freightinvoice->freightinvoice_id.'>'.$freightinvoice->FINumber.' '.$freightinvoice->CreationDate.'</option>';
            }
            $html .= '</select>';
        }
        return $html;
    }
    
    function save_noticeofarrival($post_array){
        $tempsession = $this->session->userdata('user');
        // manage freightinvoice file(copy, parsing, saving)
        $freightinvoice_filename = $this->send_XML(DOCUMENT_FREIGHTINVOICE, $post_array['file_url1'], 
                $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        $freightinvoice_location_file = './assets/documents/process/ShippingLine/'.$tempsession['username'].'/Sent/'.$freightinvoice_filename;
        $freightinvoice = $this->document_parser->convert_fixml_to_array($freightinvoice_location_file);
        $this->Freightinvoice_Entity->init(DOCUMENT_FREIGHTINVOICE,null,$freightinvoice);
        $step_1 = $this->Freightinvoice_Entity->save();
        
        // manage deliveryorder file(copy, parsing, saving)
        $deliveryorder_filename = $this->send_XML(DOCUMENT_DELIVERYORDER, $post_array['file_url2'], 
                $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        $deliveryorder_location_file = './assets/documents/process/ShippingLine/'.$tempsession['username'].'/Sent/'.$deliveryorder_filename;
        $deliveryorder = $this->document_parser->convert_doxml_to_array($deliveryorder_location_file);
        $this->Deliveryorder_Entity->init(DOCUMENT_DELIVERYORDER,null,$deliveryorder);
        $step_2 = $this->Deliveryorder_Entity->save();
        
        // manage message (message, user_message_map, message_document)
        $message['subject'] = 'noticeofarrival';
        $message['date'] = Date('Y/m/d H:i:s');
        if($post_array['quick_message'] == ""){
            $message['body'] = $post_array['body'];
        }else{
            $message['body'] = $post_array['quick_message'];
        }
        
        $message['file_url1'] = $freightinvoice_filename;
        $message['file_url2'] = $deliveryorder_filename;
        $step_3 = $this->Message_Model->insert_message($message);
        $message_id = $this->Message_Model->get_message_id_by_date($message['date']);
        $step_4 = $this->Message_Model->insert_user_message_map($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        $step_5 = $this->Message_Model->insert_attachment($message_id, DOCUMENT_FREIGHTINVOICE, $this->Freightinvoice_Entity->get_document_id(), $freightinvoice_filename);
        $step_6 = $this->Message_Model->insert_attachment($message_id, DOCUMENT_DELIVERYORDER, $this->Deliveryorder_Entity->get_document_id(), $deliveryorder_filename);
        $this->Notification_Model->insert_notification($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        return $step_1 && $step_2 && $step_3 && $step_4 && $step_5 && $step_6;
    }
    function save_freightinvoice($post_array){
        $tempsession = $this->session->userdata('user');
        // manage freightinvoice file(copy, parsing, saving)
        $freightinvoice_filename = $this->send_XML(DOCUMENT_FREIGHTINVOICE, $post_array['file_url1'], 
                $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        $freightinvoice_location_file = './assets/documents/process/ShippingLine/'.$tempsession['username'].'/Sent/'.$freightinvoice_filename;
        $freightinvoice = $this->document_parser->convert_fixml_to_array($freightinvoice_location_file);
        $this->Freightinvoice_Entity->init(DOCUMENT_FREIGHTINVOICE,null,$freightinvoice);
        $step_1 = $this->Freightinvoice_Entity->save();
        
        // manage message (message, user_message_map, message_document)
        $message['subject'] = 'freightinvoice';
        $message['date'] = Date('Y/m/d H:i:s');
        if($post_array['quick_message'] == ""){
            $message['body'] = $post_array['body'];
        }else{
            $message['body'] = $post_array['quick_message'];
        }
        $message['file_url1'] = $freightinvoice_filename;
        $step_2 = $this->Message_Model->insert_message($message);
        $message_id = $this->Message_Model->get_message_id_by_date($message['date']);
        $step_3 = $this->Message_Model->insert_user_message_map($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        $step_4 = $this->Message_Model->insert_attachment($message_id, DOCUMENT_FREIGHTINVOICE, $this->Freightinvoice_Entity->get_document_id(), $freightinvoice_filename);
        $this->Notification_Model->insert_notification($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        return $step_1 && $step_2 && $step_3 && $step_4;
    }
    function save_deliveryorder($post_array){
        $tempsession = $this->session->userdata('user');
        
        // manage deliveryorder file(copy, parsing, saving)
        $deliveryorder_filename = $this->send_XML(DOCUMENT_DELIVERYORDER, $post_array['file_url1'], 
                $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        $deliveryorder_location_file = './assets/documents/process/ShippingLine/'.$tempsession['username'].'/Sent/'.$deliveryorder_filename;
        $deliveryorder = $this->document_parser->convert_doxml_to_array($deliveryorder_location_file);
        $this->Deliveryorder_Entity->init(DOCUMENT_DELIVERYORDER,null,$deliveryorder);
        $step_1 = $this->Deliveryorder_Entity->save();
        
        // manage message (message, user_message_map, message_document)
        $message['subject'] = 'deliveryorder';
        $message['date'] = Date('Y/m/d H:i:s');
        if($post_array['quick_message'] == ""){
            $message['body'] = $post_array['body'];
        }else{
            $message['body'] = $post_array['quick_message'];
        }
        $message['file_url1'] = $deliveryorder_filename;
        $step_2 = $this->Message_Model->insert_message($message);
        $message_id = $this->Message_Model->get_message_id_by_date($message['date']);
        $step_3 = $this->Message_Model->insert_user_message_map($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        $step_4 = $this->Message_Model->insert_attachment($message_id, DOCUMENT_DELIVERYORDER, $this->Deliveryorder_Entity->get_document_id(), $deliveryorder_filename);
        $this->Notification_Model->insert_notification($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        return $step_1 && $step_2 && $step_3 && $step_4;
    }
    function save_billoflading($post_array){
        $tempsession = $this->session->userdata('user');
        // manage billoflading file(copy, parsing, saving)
        $billoflading_filename = $this->send_XML(DOCUMENT_BILLOFLADING, $post_array['file_url1'], 
                $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        $billoflading_location_file = './assets/documents/process/ShippingLine/'.$tempsession['username'].'/Sent/'.$billoflading_filename;
        $billoflading = $this->document_parser->convert_blxml_to_array($billoflading_location_file);
        $this->Billoflading_Entity->init(DOCUMENT_BILLOFLADING,null,$billoflading);
        $step_1 = $this->Billoflading_Entity->save();
        
        // manage message (message, user_message_map, message_document)
        $message['subject'] = 'billoflading';
        $message['date'] = Date('Y/m/d H:i:s');
        if($post_array['quick_message'] == ""){
            $message['body'] = $post_array['body'];
        }else{
            $message['body'] = $post_array['quick_message'];
        }
        $message['file_url1'] = $billoflading_filename;
        $step_2 = $this->Message_Model->insert_message($message);
        $message_id = $this->Message_Model->get_message_id_by_date($message['date']);
        $step_3 = $this->Message_Model->insert_user_message_map($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        $step_4 = $this->Message_Model->insert_attachment($message_id, DOCUMENT_BILLOFLADING, $this->Billoflading_Entity->get_document_id(), $billoflading_filename);
        $this->Notification_Model->insert_notification($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        return $step_1 && $step_2 && $step_3 && $step_4;
    }
    
    function save_paymentfile_freightinvoice($post_array){
        $tempsession = $this->session->userdata('user');
        // manage message (message, user_message_map, message_document)
        $payment_filename = $this->send_XML(DOCUMENT_PAYMENTFILE, $post_array['file_url1'], 
                $tempsession['role_id'], $tempsession['user_id'], ROLE_SHIPPINGLINE, $post_array['to']);
        
        /** start setting data **/
        $payment['CreationDate'] = Date('Y-m-d H-i-s');
        $payment['role_document'] = DOCUMENT_FREIGHTINVOICE;
        $payment['document_id'] = $post_array['freightinvoice'];
        $payment['file_url'] = $payment_filename;
        /** end setting data **/
        
        $this->Payment_Entity->init(DOCUMENT_PAYMENTFILE,null,$payment);
        $step_1 = $this->Payment_Entity->save();
        $message['subject'] = 'paymentfile_freightinvoice';
        $message['date'] = Date('Y/m/d H:i:s');
        if($post_array['quick_message'] == ""){
            $message['body'] = $post_array['body'];
        }else{
            $message['body'] = $post_array['quick_message'];
        }
        $message['file_url1'] = $payment_filename;
        $step_2 = $this->Message_Model->insert_message($message);
        $message_id = $this->Message_Model->get_message_id_by_date($message['date']);
        $step_3 = $this->Message_Model->insert_user_message_map($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_SHIPPINGLINE, $post_array['to']);
        $step_4 = $this->Message_Model->insert_attachment($message_id, DOCUMENT_PAYMENTFILE, $this->Payment_Entity->get_document_id(), $payment_filename);
        $this->Notification_Model->insert_notification($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_SHIPPINGLINE, $post_array['to']);
        return $step_1 && $step_2 && $step_3;
    }
    
    function save_pib($post_array){
        $tempsession = $this->session->userdata('user');
        // manage freightinvoice file(copy, parsing, saving)
        $pib_filename = $this->send_XML(DOCUMENT_PIB,$post_array['file_url1'],$tempsession['role_id'], $tempsession['user_id'],ROLE_INSW,$post_array['to']);
        $pib_location_file = './assets/documents/process/Consignee/'.$tempsession['username'].'/Sent/'.$pib_filename;
        $pib = $this->document_parser->convert_pibxml_to_array($pib_location_file);
        $this->PIB_Entity->init(DOCUMENT_PIB,null,$pib);
        $step_1 = $this->PIB_Entity->save();

        // manage message (message, user_message_map, message_document)
        
        $message['subject'] = 'pib';
        $message['date'] = Date('Y/m/d H:i:s');
        if($post_array['quick_message'] == ""){
            $message['body'] = $post_array['body'];
        }else{
            $message['body'] = $post_array['quick_message'];
        }
        $message['file_url1'] = $pib_filename;
        $step_2 = $this->Message_Model->insert_message($message);
        
        $message_id = $this->Message_Model->get_message_id_by_date($message['date']);
        $step_3 = $this->Message_Model->insert_user_message_map($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_INSW, $post_array['to']);
        $step_4 = $this->Message_Model->insert_attachment($message_id, DOCUMENT_PIB, $this->PIB_Entity->get_document_id(), $pib_filename);
        $this->Notification_Model->insert_notification($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_INSW, $post_array['to']);
        return $step_1 && $step_2 && $step_3 && $step_4;
    }
    
    function save_sppb($post_array){
        $tempsession = $this->session->userdata('user');
        
        // manage deliveryorder file(copy, parsing, saving)
        $sppb_filename = $this->send_XML(DOCUMENT_SPPB, $post_array['file_url1'], 
                5, 1, ROLE_CONSIGNEE, $post_array['to']);
        $sppb_location_file = '.././insw/assets/documents/process/INSW/'.$tempsession['username'].'/Sent/'.$sppb_filename;
        $sppb = $this->document_parser->convert_sppbxml_to_array($sppb_location_file);
        $this->SPPB_Entity->init(DOCUMENT_SPPB,null,$sppb);
        $step_1 = $this->SPPB_Entity->save();

        // manage message (message, user_message_map, message_document)
        $message['subject'] = 'sppb';
        $message['date'] = Date('Y/m/d H:i:s');
        if($post_array['quick_message'] == ""){
            $message['body'] = $post_array['body'];
        }else{
            $message['body'] = $post_array['quick_message'];
        }
        $message['file_url1'] = $sppb_filename;
        $step_2 = $this->Message_Model->insert_message($message);
        $message_id = $this->Message_Model->get_message_id_by_date($message['date']);
        $step_3 = $this->Message_Model->insert_user_message_map($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        $step_4 = $this->Message_Model->insert_attachment($message_id, DOCUMENT_SPPB, $this->SPPB_Entity->get_document_id(), $sppb_filename);
        $this->Notification_Model->insert_notification($message_id, $tempsession['role_id'], $tempsession['user_id'], ROLE_CONSIGNEE, $post_array['to']);
        return $step_1 && $step_2 && $step_3 && $step_4;
    }
    
    function save_approvement_paymentfile($document_id){
        // $document_id = id from paymentfile
        $tempsession = $this->session->userdata('user');
        // manage message (message, user_message_map, message_document)
        $payment = $this->Payment_Entity->load($document_id);
        $recipient = $this->Message_Model->get_opponent_user_by_document($tempsession['role_id'], $tempsession['user_id'], DOCUMENT_PAYMENTFILE,$document_id);
        if($payment->role_document==DOCUMENT_FREIGHTINVOICE){
            $title = 'freightinvoice';
            $document = $this->Freightinvoice_Entity->load($payment->document_id);
            $action = $document['data']->PaymentStatus;
        }
        if($action==ACTION_APPROVE){
            $action = 'approved';
        }else if($action==ACTION_REJECT){
            $action = 'rejected';
        }
        $message['subject'] = 'approvement-'.$title;
        $message['date'] = Date('Y/m/d H:i:s');
        $message['body'] = 'Your '.$title.' has been '.$action;
        $message['body'] .= '<br />';
        $message['body'] .= "<a target='_blank' href=".base_url()."message/view_document_page/".$payment->role_document."/".$payment->document_id.">view ".$title."</a>";
        $step_1 = $this->Message_Model->insert_message($message);
        $message_id = $this->Message_Model->get_message_id_by_date($message['date']);
        $step_2 = $this->Message_Model->insert_user_message_map($message_id, $tempsession['role_id'], $tempsession['user_id'], $recipient->role_user_id, $recipient->user_id);
        $this->Notification_Model->insert_notification($message_id, $tempsession['role_id'], $tempsession['user_id'], $recipient->role_user_id, $recipient->user_id);
        return $step_1 && $step_2;
    }
    
    function save_approvement($role_doc, $doc_id){
        $tempsession = $this->session->userdata('user');
        $recipient = $this->Message_Model->get_opponent_user_by_document($tempsession['role_id'], $tempsession['user_id'], $role_doc, $doc_id);
        if($role_doc==DOCUMENT_FREIGHTINVOICE){
            $title = 'freightinvoice';
            $document = $this->Freightinvoice_Entity->load($doc_id);
            $action = $document['data']->ApprovementStatus;
        }else if($role_doc==DOCUMENT_DELIVERYORDER){
            $title = 'deliveryorder';
            $document = $this->Deliveryorder_Entity->load($doc_id);
            $action = $document['data']->ApprovementStatus;
        }else if($role_doc==DOCUMENT_PIB){
            $title = 'pib';
            $document = $this->PIB_Entity->load($doc_id);
            $action = $document['data']->ApprovementStatus;
        }else if($role_doc==DOCUMENT_SPPB){
            $title = 'sppb';
            $document = $this->SPPB_Entity->load($doc_id);
            $action = $document['data']->ApprovementStatus;
        }
        if($action==ACTION_APPROVE){
            $action = 'approved';
        }else if($action==ACTION_REJECT){
            $action = 'rejected';
        }
        $message['subject'] = 'approvement-'.$title;
        $message['date'] = Date('Y/m/d H:i:s');
        $message['body'] = 'Your '.$title.' has been '.$action;
        $message['body'] .= '<br />';
        $message['body'] .= "<a target='_blank' href=".base_url()."message/view_document_page/".$role_doc."/".$doc_id.">view ".$title."</a>";
        $step_1 = $this->Message_Model->insert_message($message);
        $message_id = $this->Message_Model->get_message_id_by_date($message['date']);
        $step_2 = $this->Message_Model->insert_user_message_map($message_id, $tempsession['role_id'], $tempsession['user_id'], $recipient->role_user_id, $recipient->user_id);
        $this->Notification_Model->insert_notification($message_id, $tempsession['role_id'], $tempsession['user_id'], $recipient->role_user_id, $recipient->user_id);
        return $step_1 && $step_2;
    }
    
    function save_message($post_array){
        $tempsession = $this->session->userdata('user');
        // manage message (message, user_message_map, message_document)
        $to = explode(',',$post_array['to']);
        $to_role_user = $to[0];
        $to_user = $to[1];
        $message['subject'] = $post_array['subject'];
        $message['date'] = Date('Y/m/d H:i:s');
        if($post_array['quick_message'] == ""){
            $message['body'] = $post_array['body'];
        }else{
            $message['body'] = $post_array['quick_message'];
        }
        $step_1 = $this->Message_Model->insert_message($message);
        $message_id = $this->Message_Model->get_message_id_by_date($message['date']);
        $step_2 = $this->Message_Model->insert_user_message_map($message_id, $tempsession['role_id'], $tempsession['user_id'], $to_role_user, $to_user);
        $this->Notification_Model->insert_notification($message_id, $tempsession['role_id'], $tempsession['user_id'], $to_role_user, $to_user);
        return $step_1 && $step_2;
    }
    
    function call_view_message_outbox_page($primary_key , $row)
    {
        return site_url('message/view_message_page').'/'.$primary_key;
    }
    function call_view_message_inbox_page($primary_key , $row)
    {
        return site_url('message/view_message_page').'/'.$primary_key;
        //return site_url('message/view_message_page').'/inbox/'.$row->message_id;
    }
    
    public function test_download(){
        $this->download_document(2,DOCUMENT_SPPB,1,"docx",null);
    }
}
